#! /usr/bin/perl

#package to hold a collection of html based subroutines that will be used throughout the project
package HTMLSubs;


#enable strict mode
use strict;
use DBQueries;


#subroutine to generate the html code to create an html file up to the opening body tag
#furthermore, this subroutine sets the type of printing to be done as html until changed
#this subroutine accepts a scalar to be passed as the website title
sub generate_html_beginning{
	
	#since this is a different module, the first argument will be the class
	#therefore call the second parameter
	my $title = $_[1];
	
	# set up to print correct content
	print "Content-type: text/html\n\n";
	
	# now print the tags
	print <<END_HTML;

	<html>
		<head>
			<title>$title</title>
			<link href="style.css" type= "text/css" rel="stylesheet" />
		</head>

	<body>
END_HTML
}

#subroutine to generate the header of pages inlcuding the graphics and basic user info
sub generate_display_head{
	
	#obtain parameters
	my $main_title = $_[1];
	my $sub_title = $_[2];
	my $username = $_[3];
	
	#create a hash containing only the username key/value pair to be passed to the button creation subroutine
	my %username_hash;
	$username_hash{'username'} = $username;
	
	#obtain general user info from DB
	#declare scalars to hold most important information
	my $groups_owned = scalar(DBQueries::getUserCreatedGroups($username));
	my $groups_member = scalar(DBQueries::getUserGroups($username));
	my $files_owned = scalar(DBQueries::getUserFileNames($username));
	my $files_seeable = scalar(DBQueries::getAllUserFileNames($username));
	my($first_name, $last_name) = DBQueries::getUserInfo($username);
	
	#create the main outer table, subtables to be filled by further subroutines 
	#this html code is split up into bits so that respecitve subroutines can be called at the correct times
	print <<END_HTML;
	
	<div class="header">
	<div class="titles">
	<h1>$main_title</h1>
	</div>
	
	<!--titles-->
	<h2>$sub_title</h2>
	
	<h3>Logged in as $first_name $last_name </h3>
	
	<table class="tableheader">
	<tr class="trheader">
	<td class="tdheaderleft">
	
	<!--general stats-->
	<p>Own $files_owned Files (Access to $files_seeable Files) | Own $groups_owned Groups (Member of $groups_member Groups)</p></td>
	
	<!--now display the main navigation buttons-->
	<td class="tdheaderright">
	<table class="googletablebuttons1" cellpadding="0" cellspacing="0" style="float:right;">
	<tr class="googletrbuttons1">
	<td>
END_HTML

	#now call a subroutine to print the html code for a button as well as providing the script with the necessary information
	HTMLSubs -> generate_html_button("updateUser.pl.cgi", "Edit User Information", \%username_hash);
	
	#proceed with the table tags
	print "</td><td>";
	
	#create the button for uploading a file
	HTMLSubs -> generate_html_button("uploadFile.pl.cgi", "Upload a File", \%username_hash);
	
	#proceed with the table tags
	print "</td><td>";
	
	#button for creating a group
	HTMLSubs -> generate_html_button("newGroup.pl.cgi", "Create a new Group", \%username_hash);
	
	#proceed with the table tags
	print "</td><td>";
	
	#button for searchign file content
	HTMLSubs -> generate_html_button("searchFiles.pl.cgi", "Search Files", \%username_hash);
	
	#use the block print for the following code
	print <<END_HTML;
	<td>
	<!--also add logout button-->
	<form method="LINK" action="login.pl.cgi"><input type="submit" value="Logout"></form>
	</td>
	</td>
	</tr>
	</table>
	</td>
	</tr>
	</table>	
	</div><!-- end of header of the profile header-->
	
END_HTML
}

#subroutine to generate the html code to end an html file (close body and html tags)
sub generate_html_end{
	
	# print the tags
	print <<END_HTML;

	<div class="footer">
	Created by Nathaniel Clinger and Thomas Rantasa
	</div><!--footer-->
	</body>
	</html>
END_HTML
}

#subroutine to split the full form information string into a hash of keys and values passed to the script
#this subroutine is passed a full input string from POST or GET as a parameter
sub decode_input{
	
	#since this is a different module, the first argument will be the class
	#therefore call the second parameter
	my $inputstring = $_[1];
	
	#declare hash to hold all key value pairs passed to the script
	#this method will handle any arbitrary number of arguments passed to the script and can therefore be used universally
	my ($pair,%forminfo);

	#declare variables to temporarily hold key and value
	my($key, $value);

	#now loop through inputstring and process all infomration passed to the script
	foreach $pair (split(/&/, $inputstring)){

		#split around = sign
		($key, $value) = split(/=/, $pair);

		#now add the pair to the hash storing all information
		$forminfo{"$key"} = $value;
	} # end foreach to decode input
	
	#now return the reference to the hash with all input
	return \%forminfo;
}

#subroutine to create html code for a link button
#this sub takes a target and button text as a parameter
#also, this sub accepts a hash reference as a third parameter that allows for key/value pairs to be sent to the target script
sub generate_html_button{
	
	#obtain parameters
	my $target = $_[1];
	my $text = $_[2];
	my $parameter_ref = $_[3] || "";
	
	#print the beginnign of the form
	print "<form action=\"$target\" method=POST>";
	
	#if the hash ref exits, dereference
	#dereference or create empty hash if no reference exits
	my %parameters;
	
	#check if refernce is empty
	if(length($parameter_ref) == 0){
		
		#in this case create an empty hash
		%parameters = {};
	}
	#otherwise use reference
	else{
		%parameters = %$parameter_ref;
	}
	
	#loop through hash and create hidden fields for all parameters
	foreach my $key(keys %parameters){
		
		#print field
		print "<input type=\"hidden\" name=\"$key\" value=\"$parameters{\"$key\"}\">";
	}

	#now print the submit button and end form tag
	print"<input type=\"submit\" value=\"$text\"></form>";

}

#subroutine to update the log file
sub update_log{
	
	#obtain message from parameter
	my $log_addition = $_[1];
	
	#open the log file for appending
	open(FH, ">>files/log\.txt");
	
	#now print to the filehandle
	print FH "$log_addition\n";
	
	#close the file
	close(FH);
}


#return 1 to ensure true value of module
1;